import request from '@/utils/request'

export function onPrint() {
  const printNode = this.$refs.print
  if(!printNode) return
  const newIframe = document.createElement('iframe');
  newIframe.setAttribute('style', 'width:0px;height:0px;position:absolute;left:-9999px;top:-9999px;');
  newIframe.setAttribute('align', 'center');
  document.body.appendChild(newIframe);
  let doc = null;
  doc = newIframe.contentWindow.document;
  doc.write(`
    <style type="text/css">
      @media print {
        @page {
          size: auto;
          margin: 1mm;
          table { page-break-after: auto; }
          tr    { page-break-inside: avoid; page-break-after: auto; }
          td    { page-break-inside: avoid; page-break-after: auto; }
          thead { display: table-header-group; }
          tfoot { display: table-footer-group; }
        }
      }
      .print-preview { display: flex; flex-direction: column; color: rgba(51, 51, 51, 1); font-family: Microsoft YaHei, '黑体', STSong, '宋体',Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono; }
      .title { display: flex; justify-content: center; font-size: 21px; height: 50px; line-height: 50px; font-weight: bold; }
      .divide_line { justify-content: center; border-bottom: 1px dashed rgba(51, 51, 51, 1); }
    </style>
    <div style="width:100%;height:auto;width:320px;margin:0px auto;"align="center">
      ${printNode.innerHTML}
    </div>`)
  doc.close();
  setTimeout(() => {
    newIframe.contentWindow.focus();
    newIframe.contentWindow.print();
    document.body.removeChild(newIframe);
  }, 10);
}
